package com.lzx.dormsys.service.impl;

import com.github.pagehelper.PageInfo;
import com.lzx.dormsys.bean.MenuBean;
import com.lzx.dormsys.mapper.MenuBeanMapper;
import com.lzx.dormsys.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    MenuBeanMapper menuBeanMapper;

    @Override
    public int insert(MenuBean menuBean) {
        return 0;
    }

    @Override
    public int update(MenuBean menuBean) {
        return 0;
    }

    @Override
    public List<MenuBean> queryData(MenuBean menuBean) {
        return null;
    }

    @Override
    public PageInfo<MenuBean> queryData(MenuBean menuBean, int pageSize, int pageNum) {
        return null;
    }

    @Override
    public int deleteByPrimaryKey(MenuBean menuBean) {
        return 0;
    }

    @Override
    public int deleteByPrimaryKey(String id) {
        return 0;
    }

    @Override
    public List<MenuBean> queryMenuByuserid(String userid) {

        return menuBeanMapper.queryMenuByuserid(userid);
    }

    @Override
    public List<Map<String, Object>> queryAllMenus() {

        /**
         * [
         *    {menuid:'m1',title:"主页",icon:"el-icon-location",children:[{menuid:'m11',title:"控制台",routerURL:"/home/wellcome"}]},
         *    {menuid:'m2',title:"系统设置",icon:"el-icon-location",children:[{menuid:'m22',title:"管理员列表",routerURL:"/home/userlist"}]},
         *    {menuid:'m4',title:"系统设置",icon:"el-icon-location",children:[{menuid:'m24',title:"管理员列表",routerURL:"/home/userlist"}]}
         * ]
         */


        List<MenuBean> menuBeanList = menuBeanMapper.selectByExample(null);
        //判断List<MenuBean> 中的paremntMenuid属性值是否为空，为空的做为一级菜单列表
        //不为空的parentMenuid就放到与之匹配的menuid对应children列表

        List<Map<String, Object>> returnValues = new ArrayList<>();

        for (MenuBean menuBean : menuBeanList) {
            if (menuBean.getParentmenuid() == null || menuBean.getParentmenuid().trim().equals("")) {
                Map<String, Object> level_1 = new HashMap<>();
                level_1.put("id", menuBean.getMenuid());
                level_1.put("label", menuBean.getTitle());

                returnValues.add(level_1);

                List<Map<String, Object>> level2MapList = new ArrayList<>();
                for (MenuBean subMenuBean : menuBeanList) {//找与menuBean的menuid匹配的parentMenuid
                    if (subMenuBean.getParentmenuid() != null && subMenuBean.getParentmenuid().equals(menuBean.getMenuid())) {
                        Map<String, Object> level_2 = new HashMap<>();
                        level_2.put("id", subMenuBean.getMenuid());
                        level_2.put("label", subMenuBean.getTitle());
                        level2MapList.add(level_2);
                    }
                }
                level_1.put("children", level2MapList);
            }
        }
        return returnValues;
    }
}
